//
//  BankLocationsFooterView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/28.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class BankLocationsFooterView: UIView {
    
    var model : BankDetailsModel! {
        didSet{
           getData()
        }
    }
    
    var isFlag = false //是否显示尾部详情
    
    let nameButton = UIButton()
    fileprivate let nameLabel = UILabel() //银行名称
    fileprivate let distanceLabel = UILabel() //距离
    fileprivate let addressLabel = UILabel() //地址
    fileprivate let phoneLabel = UILabel() //联系电话
    let goButton = UIButton() //去这里

    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.white
        
        nameButton.layer.borderColor = UIColor.lightGray.cgColor
        nameButton.layer.borderWidth = 1
        self.addSubview(nameButton)
        
        nameLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        nameButton.addSubview(nameLabel)
        
        distanceLabel.textAlignment = .right
        distanceLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: RATIO(maxNum: 14)))
        nameButton.addSubview(distanceLabel)
        
        addressLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.addSubview(addressLabel)
        
        goButton.setTitleColor(UIColor.blue, for: .normal)
        goButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        goButton.titleLabel?.textAlignment = .right
        self.addSubview(goButton)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.mineColor()
        self.addSubview(lineView)
        
        phoneLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.addSubview(phoneLabel)
        
        nameButton.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 50))
        }
        
        distanceLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.height.greaterThanOrEqualTo(10)
            make.width.equalTo(50)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
        make.right.equalTo(distanceLabel.snp.left)
            make.height.greaterThanOrEqualTo(10)
        }
        
        goButton.snp.makeConstraints { (make) in
        make.top.equalTo(nameButton.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.width.equalTo(RATIO(maxNum: 50))
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        addressLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.centerY.equalTo(goButton)
            make.right.equalTo(goButton.snp.left).offset(RATIO(maxNum: -10))
            make.height.greaterThanOrEqualTo(10)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        make.top.equalTo(goButton.snp.bottom).offset(RATIO_H(maxNum: 10))
        }
        
        phoneLabel.snp.makeConstraints { (make) in
        make.top.equalTo(lineView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.equalTo(addressLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
    }
    
    //MARK: 更新数据
    fileprivate func getData() {
        
        nameLabel.text = model.name
        if model.distance! > 1000 {
            let km = "\(Double(model.distance!)/1000)"
            distanceLabel.text = String(format: "%.1f", Double(km) ?? 0) + "km"
        }else{
            distanceLabel.text = "\(model.distance!)" + "m"
        }
        addressLabel.text = model.address
        goButton.setTitle(goHere, for: .normal)
        phoneLabel.text = model.tel ?? ""
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}
